﻿using ComLib.ComFun;
using ComLib.ConfigService;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

namespace PrintExcel
{
    ////////////////////////////////////////////////////////////////////////////
    // 基础数据结构
    ////////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// Excel单元格信息数据结构
    /// </summary>
    public enum ItemType : int
    {
        Text = 0,
        CheckBox = 1,
        Picture = 2,
        UndeLine = 3,
        BarCode = 4,
        PackageImage = 5,
    }


    /// <summary>
    /// Excel单元格信息数据结构
    /// </summary>
    public class RangeItemInfo
    {
        /// <summary>
        /// 数据项Range 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 数据项Range 元素类型
        /// </summary>
        public ItemType Type { get; set; }
    
        /// <summary>
        /// 数据项Range 左上区域名
        /// </summary>
        public string Start { get; set; }
        /// <summary>
        /// 数据项Range 右下区域名
        /// </summary>
        public string End { get; set; }
        /// <summary>
        /// 正反面
        /// </summary>
        public string Side { get; set; }

        /// <summary>
        /// 数据项Range 位置信息
        /// </summary>
        public float Top { get; set; }
        public float Left { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public float OffsetX { get; set; }
        public float OffsetY { get; set; }
        public float Scale { get; set; }

        public RangeItemInfo()
        {
            this.Reset();
        }

        public void Reset()
        {
            Top = 0F;
            Left = 0F;
            Width = 0;
            Height = 0;
            OffsetX = 0F;
            OffsetY = 0F;
            Scale = 1.0F;
            Type = ItemType.Text;
            Name = "";
            Start = "";
            End = "";
            Side = "front";         
        }
    }
    [Serializable]
    [XmlRootAttribute("PaperPrinterOption", IsNullable = false)]
    public class PaperPrinterOption : XmlSerializeBase
    {
        [XmlIgnore]
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        /// <summary>
        /// 打印机 名称
        /// </summary>
        public string Printer { get; set; }

        /// <summary>
        /// 打印机PID
        /// </summary>
        public string VID { get; set; }

        /// <summary>
        /// 打印机VID
        /// </summary>
        public string PID { get; set; }

        /// <summary>
        /// 超时时间
        /// </summary>
        public int Timeout { get; set; }

        /// <summary>
        /// 是否打印头像
        /// </summary>
        public bool IsPrintPerson { get; set; }

        /// <summary>
        /// 设备名称
        /// </summary>
        public string DeviceName { get; set; }

        /// <summary>
        /// 设备编号
        /// </summary>
        public string DeviceNo { get; set; }

        public PaperPrinterOption():base()
        {
            DeviceNo = "001";
            DeviceName = "预受理一体机";
            Timeout = 8000;
            PID = "03F0";
            VID = "152A";
            Printer = "HP LaserJet Pro M304-M305 PCL6(V3)";
            IsPrintPerson = true;
        }

        public override bool Serialize()
        {
            return Serializer.SaveToXml(ConfigFilePath, this);
        }

        public override bool Load()
        {
            LogLib.Log.WriteAsync($"打印配置路径:{ConfigFilePath}");
            PaperPrinterOption obj = (PaperPrinterOption)Serializer.LoadFromXml(ConfigFilePath, typeof(PaperPrinterOption));
            if (obj != null)
            {
                this.PID = obj.PID;
                this.Printer = obj.Printer;
                this.VID = obj.VID;
                this.Timeout = obj.Timeout;
                this.IsPrintPerson = obj.IsPrintPerson;
                this.DeviceName = obj.DeviceName;
                this.DeviceNo = obj.DeviceNo;
                this._nVer = obj._nVer;

                LogLib.Log.WriteAsync($"打印配置:{this.ToString()}");
                return true;
            }
            else
            {
                ErrorMessage = Serializer.Message;
                return false;
            }
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("申请表格打印选项: ");
            Action<string, object> FillString =
                (string field, object val) =>
                {
                    if (val != null)
                    {
                        builder.Append("[");
                        builder.Append(field);
                        builder.Append("]: ");
                        builder.Append(val.ToString() + "; ");
                    }
                };
            FillString("是否打印头像", IsPrintPerson ? "是" : "否");
            FillString("VID", VID);
            FillString("PID", PID);
            FillString("Name", Printer);
            FillString("超时时间", Timeout);
            FillString("设备名称", DeviceName);
            FillString("设备编号", DeviceNo);
            return builder.ToString();
        }
    }
}
